﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Security.Cryptography;
using System.Text;

namespace Sanctuary.Core
{
    public class Security
    {
        public static byte[] Hash(string value, byte[] salt)
        {
            return Hash(Encoding.UTF8.GetBytes(value), salt);
        }

        public static byte[] Hash(byte[] value, byte[] salt)
        {
            byte[] saltedValue = value.Concat(salt).ToArray();
            return new SHA256Managed().ComputeHash(saltedValue);
        } 

        public bool ConfirmPassword(string password, byte[] salt, byte[] hashedSaltedPassword) 
        { 
            byte[] passwordHash = Hash(password, salt);
            return hashedSaltedPassword.SequenceEqual(passwordHash); 
        } 

        public static byte[] CreateSalt(int size)
        {
            // Generate a cryptographic random number using the cryptographic service provider
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] buff = new byte[size];
            rng.GetBytes(buff);
            return buff;
        }
    }
}
